<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../../styles/common.css" />
    <link rel="stylesheet" href="../../styles/page.css" />
    <style>
        .selectInput {
            position: relative;
            width: 100%;
            min-width: 100px;
        }
        .selectInput .flavorSelect {
            position: absolute;
            width: 100%;
            padding: 0 10px;
            border-radius: 3px;
            border: solid 1px #00FA9A;
            line-height: 30px;
            text-align: center;
            background: #fff;
            top: 50px;
            z-index: 99;
        }
        .selectInput .flavorSelect .items {
            cursor: pointer;
            display: inline-block;
            width: 100%;
            line-height: 35px;
            border-bottom: solid 1px #f4f4f4;
            color: #666;
        }
        .selectInput .flavorSelect .none {
            font-size: 14px;
        }

        #news-add-app .uploadImg .el-form-item__label::before{
            content: '*';
            color: #00FA9A;
            margin-right: 4px;
        }

    </style>
</head>
<body>
<div class="addBrand-container" id="news-add-app">
    <div class="container">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" :inline="true" label-width="180px" class="demo-ruleForm">
            <div>
                <el-form-item label="新闻标题:" prop="title">
                    <el-input v-model="ruleForm.title" placeholder="请填写新闻标题" maxlength="20"/>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="图片:" prop="region" class="uploadImg">
                   <el-upload
                   			  class="avatar-uploader"
                   			  action="/apis/common/upload"
                   			  :show-file-list="false"
                   			  :on-success="handleAvatarSuccess"
                   			  :before-upload="beforeAvatarUpload">
                   			  <img v-if="imageUrl" :src="imageUrl" class="avatar">
                   			  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                   			</el-upload>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="新闻内容:" prop="title">
                    <el-input type="textarea"
                              :autosize="{ minRows: 4 }"
                              v-model="ruleForm.sysNewsDetail.content"
                              placeholder="请填写新闻内容"></el-input>
                </el-form-item>
            </div>
            <div>
                <el-form-item label="类型:" prop="type">
                    <el-input v-model="ruleForm.type" placeholder="请填写新闻类型" maxlength="20"/>
                </el-form-item>
            </div>
            <!--        <div>-->
            <!--          <el-form-item label="发布时间:" prop="publishTime">-->
            <!--          	<el-date-picker-->
            <!--                v-model="ruleForm.publishTime"-->
            <!--                type="date"-->
            <!--                placeholder="选择日期">-->
            <!--              </el-date-picker>-->
            <!--          </el-form-item>-->
            <!--        </div>-->

            <div class="subBox address">
                <el-form-item>
                    <el-button @click="goBack()">取消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script src="../../api/news.js"></script>
<script>
    new Vue({
        el: '#news-add-app',
        data() {
            return {
                id: '',
                restKey: 0,
                imageUrl:'',
                actionType: '',
                index : 0,
                inputStyle  : {'flex':1},
                ruleForm  : {
                    'newsId': '',
                    'userId': '',
                    'title': '',
                    'image': '',
                    'sysNewsDetail':{
                        'content': ''
                    },
                    'type': '',
                    'publishTime': '',
                    'remark': ''
                },
                mak: false,
            }
        },
        computed: {
            rules () {
                return {
                    'title': [
                        {'required': true, 'message': '请填写标题', 'trigger': 'blur'}
                    ],
                }
            }
        },
        created() {
            //和修改相关
            this.id = requestUrlParam('id');
            this.actionType = this.id ? 'edit' : 'add';
            if (this.id) {
                this.init()
            }
            var _this = this

        },
        mounted() {
        },
        methods: {
			handleAvatarSuccess(res, file) {
				console.log(res.data);
				this.ruleForm.image=res.data
				console.log(this.ruleForm.image);
				console.log(file);
				this.imageUrl = URL.createObjectURL(file.raw);
			},
            async init () {
                var _this = this
                queryNewsById(this.id).then(res => {
                    console.log(res);
                    if (String(res.code) === '1') {
                        this.ruleForm = { ...res.data };
						this.imageUrl = $minioUrl+res.data.image
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            },
            submitForm(formName, st) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let params = {...this.ruleForm};
                        editNews(params).then(res => {
                            if (res.code === 1) {
                                this.$message.success('新闻编辑成功！');
                                if (!st) {
                                    this.goBack()
                                }
                            } else {
                                this.$message.error(res.msg || '操作失败')
                            }
                        }).catch(err => {
                            this.$message.error('请求出错了：' + err)
                        })
                    } else {
                        return false
                    }
                })
            },
            onChange (file) {
                if(file){
                    const suffix = file.name.split('.')[1];
                    const size = file.size / 1024 / 1024 < 2;
                    if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！');
                        this.$refs.upload.clearFiles();
                        return false
                    }
                    if(!size){
                        this.$message.error('上传文件大小不能超过 2MB!');
                        return false
                    }
                    return file
                }
            },

            goBack(){
                window.parent.menuHandle({
                    id: '9',
                    url: 'page/news/list.html',
                    name: '新闻管理'
                },false)
            }
        }
    })
</script>
</body>
</html>